package com.viettel.pon.httpclient;

import java.io.ByteArrayInputStream;
import java.io.File;

import android.util.Log;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.turbomanage.httpclient.AsyncCallback;
import com.turbomanage.httpclient.ParameterMap;
import com.turbomanage.httpclient.android.AndroidHttpClient;

public class HttpBasicClientHelper {

    // duong dan chung

    public static final String BASE_URL = "http://10.58.65.188:8082/PONWebservice/resource/";
    
    
    // DONG BO DU LIEU
    public static final String SYNC_HOUSE = "";
    public static final String SYNC_POLE = "";
    public static final String SYNC_STREET = "";
    public static final String SYNC_PLACE = "";


    private static AndroidHttpClient client = new AndroidHttpClient();
    private AsyncCallback asyncCallback;
    private MyJsonHttpResponseHandler handler;

    public HttpBasicClientHelper(AsyncCallback asyncCallback) {
	this.asyncCallback = asyncCallback;
    }
    
    public HttpBasicClientHelper(MyJsonHttpResponseHandler handler) {
	this.handler = handler;
    }

    public static void get(final String url, final AsyncCallback responseHandler, final ParameterMap params) {
	Log.i("Tuan", url);
	new Thread(new Runnable() {

	    public void run() {
		client.get(getAbsoluteUrl(BASE_URL, url), params, responseHandler);
	    }
	}).start();

    }

    public static void get(String url, AsyncCallback responseHandler) {
	Log.i("Tuan", url);
	client.get(url, null, responseHandler);
    }

//    public static void uploadImage(final String url, final AsyncCallback responseHandler, final RequestParams params, byte[] byteArray, MyJsonHttpResponseHandler jsonHandler) {
////	RequestParams params = new RequestParams();
////	params.put("sso_id", preferenceHelper.getUserId());
////	params.put("hash", preferenceHelper.getHash());
////
////	params.put("picture", new ByteArrayInputStream(byteArray), "image.jpg");
//
//	AsyncHttpClient client = new AsyncHttpClient();
//	client.
//	client.post("http://vchat.vn/service/upload_image.php", params, new MyJsonHttpResponseHandler() {
//
//	    @Override
//	    public void onFailure(Throwable error) {
//		super.onFailure(error);
////		Toast.makeText(ChatActivity.this, "Request failed.", Toast.LENGTH_SHORT).show();
//	    }
//
//	    @Override
//	    public void onSuccess(JSONObject re) {
//		try {
//		    String error = re.getString("error");
//
//		    if (!error.equals("")) {
//			Log.i("DATA", "File qua lon");
//		    } else {
//			String data = re.getString("data");
//			Log.i("DATA", "URL tra ve: " + data);
//		    }
//
//		} catch (JSONException e) {
//		    e.printStackTrace();
//		}
//	    }
//	});
//    }

    public static void post(final String url, final AsyncCallback responseHandler, final ParameterMap params) {
	new Thread(new Runnable() {

	    public void run() {
		client.setConnectionTimeout(600000);
		client.post(getAbsoluteUrl(BASE_URL, url), params, responseHandler);

	    }
	}).start();

	Log.i("URL", "" + getAbsoluteUrl(BASE_URL, url));

    }

    public static void postWithHeader(String url, AsyncCallback responseHandler, ParameterMap params) {

	/*
	 * try { String credentials = "vchat-mobile" + ":" +
	 * "I2o5VG0c5a5dnZ866Y8Sguthojavuw"; String base64EncodedCredentials =
	 * Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
	 * client.addHeader("Authorization", "Basic " +
	 * base64EncodedCredentials);
	 * 
	 * client.post(getAbsoluteUrl(BASE_URL_VATGIA,url), params,
	 * responseHandler); } catch (Exception e) { e.printStackTrace(); }
	 */

    }

    private static String getAbsoluteUrl(String base, String relativeUrl) {
	return base + relativeUrl;
    }

    
    public void syncHouse(String jsonStr){
    	ParameterMap params = new ParameterMap();
    	params.put("", jsonStr);
    	post(SYNC_HOUSE, asyncCallback, params);
    }
    
    public void syncPole(String jsonStr){
    	ParameterMap params = new ParameterMap();
    	params.put("listPoles", jsonStr);
    	post(SYNC_POLE, asyncCallback, params);
    }
    
    public void syncStreet(String jsonStr){
    	ParameterMap params = new ParameterMap();
    	params.put("", jsonStr);
    	post(SYNC_STREET, asyncCallback, params);
    }
    
    public void syncPlace(String jsonStr){
    	ParameterMap params = new ParameterMap();
    	params.put("", jsonStr);
    	post(SYNC_PLACE, asyncCallback, params);
    }

}
